package com.kmproject.haifu.mvp.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

/**
 * Description:圆角ImageView
 * Created by Kevin.Li on 2019-08-16.
 */
public class RoundCornerImageView extends android.support.v7.widget.AppCompatImageView {
    //圆角大小，默认为10
    private int mBorderRadius = 10;

    // 3x3 矩阵，主要用于缩小放大
    private Matrix mMatrix;

    private CornerType mCornerType = CornerType.ALL;

    public RoundCornerImageView(Context context) {
        this(context, null);
    }

    public RoundCornerImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));

        mMatrix = new Matrix();
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    public RoundCornerImageView setmBorderRadius(int mBorderRadius) {
        this.mBorderRadius = mBorderRadius;
        invalidate();
        return this;
    }

    public RoundCornerImageView setmCornerType(CornerType mCornerType) {
        this.mCornerType = mCornerType;
        invalidate();
        return this;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null) {
            return;
        }
        Bitmap bitmap = drawableToBitmap(getDrawable());
        //渲染图像，使用图像为绘制图形着色
        BitmapShader mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale = 1.0f;
        if (!(bitmap.getWidth() == getWidth() && bitmap.getHeight() == getHeight())) {
            // 如果图片的宽或者高与view的宽高不匹配，计算出需要缩放的比例；缩放后的图片的宽高，一定要大于我们view的宽高；所以我们这里取大值；
            scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(),
                    getHeight() * 1.0f / bitmap.getHeight());
        }
        // shader的变换矩阵，我们这里主要用于放大或者缩小
        mMatrix.setScale(scale, scale);
        // 设置变换矩阵
        mBitmapShader.setLocalMatrix(mMatrix);
        // 设置shader
//        mPaint.setShader(mBitmapShader);
//        canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), mBorderRadius, mBorderRadius,
//                mPaint);
        drawRoundCorner(canvas, mBitmapShader, getWidth(), getHeight(), mBorderRadius);
    }

    private void drawRoundCorner(Canvas canvas, BitmapShader bitmapShader, int width, int height, int radius) {
        float[] outerR;
        switch (mCornerType) {
            case LEFT_TOP:
                outerR = new float[]{radius, radius, 0, 0, 0, 0, 0, 0};
                break;
            case LEFT_BOTTOM:
                outerR = new float[]{0, 0, 0, 0, 0, 0, radius, radius};
                break;
            case RIGHT_TOP:
                outerR = new float[]{0, 0, radius, radius, 0, 0, 0, 0};
                break;
            case RIGHT_BOTTOM:
                outerR = new float[]{0, 0, 0, 0, radius, radius, 0, 0};
                break;
            case LEFT:
                outerR = new float[]{radius, radius, 0, 0, 0, 0, radius, radius};
                break;
            case RIGHT:
                outerR = new float[]{0, 0, radius, radius, radius, radius, 0, 0};
                break;
            case BOTTOM:
                outerR = new float[]{0, 0, 0, 0, radius, radius, radius, radius};
                break;
            case TOP:
                outerR = new float[]{radius, radius, radius, radius, 0, 0, 0, 0};
                break;
            case ALL:
            default:
                outerR = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
                break;
        }

        ShapeDrawable mDrawables = new ShapeDrawable(new RoundRectShape(outerR, null, null));
        mDrawables.getPaint().setShader(bitmapShader);
        mDrawables.setBounds(0, 0, width, height);
        mDrawables.draw(canvas);
    }

    private Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }
        // 当设置不为图片，为颜色时，获取的drawable宽高会有问题，所有当为颜色时候获取控件的宽高
        int w = drawable.getIntrinsicWidth() <= 0 ? getWidth() : drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight() <= 0 ? getHeight() : drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }

    /**
     * 圆角方向
     */
    public enum CornerType {
        /**
         * 所有角
         */
        ALL,
        /**
         * 左上
         */
        LEFT_TOP,
        /**
         * 左下
         */
        LEFT_BOTTOM,
        /**
         * 右上
         */
        RIGHT_TOP,
        /**
         * 右下
         */
        RIGHT_BOTTOM,
        /**
         * 左侧
         */
        LEFT,
        /**
         * 右侧
         */
        RIGHT,
        /**
         * 下侧
         */
        BOTTOM,
        /**
         * 上侧
         */
        TOP
    }
}
